package com.dishdex.menuhub.module.module.dish.service;

import com.dishdex.menuhub.module.module.dish.entity.Dish;
import com.dishdex.menuhub.module.module.dish.request.DishDTO;
import com.dishdex.menuhub.module.module.dish.mapper.DishMapper;

import javax.annotation.Resource;

import com.dishdex.menuhub.module.module.category.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:
 * Package: com.dishdex.menuhub.service
 * Description:
 *
 * @Author:
 * @Create 2025/8/1 15:12
 * @Version 1.0
 */
@Service
public class DishService {

    @Resource
    private DishMapper dishMapper;

    public Dish getById(Long dishId) {
        if (dishId == null) {
            throw new RuntimeException("Id不能为空");
        }
        if (dishId <= 0) {
            throw new RuntimeException("dishId需要大于0");
        }
        Dish dish = dishMapper.getById(dishId);
        if (dish == null) {
            throw new RuntimeException("菜品不存在");
        }
        return dish;
    }

    public Dish extractById(Long dishId) {
        if (dishId == null) {
            throw new RuntimeException("Id不能为空");
        }
        if (dishId <= 0) {
            throw new RuntimeException("dishId需要大于0");
        }
        Dish dish = dishMapper.extractById(dishId);
        if (dish == null) {
            throw new RuntimeException("菜品不存在");
        }
        return dish;
    }

    @Resource
    private CategoryService categoryService;

    public Long edit(Long dishId,
                     String coverImages,
                     String dishName,
                     Float dishPrice,
                     String dishIngredients,
                     String dishDescription,
                     Long categoryId) {

        if (coverImages == null || coverImages.trim().isEmpty()) {
            throw new RuntimeException("coverImages不能为空");
        }
        if (coverImages.trim().length() > 1000) {
            throw new RuntimeException("coverImages的长度不能超过1000");
        }
        if (dishName == null || dishName.trim().isEmpty()) {
            throw new RuntimeException("dishName不能为空");
        }
        if (dishName.trim().length() > 100) {
            throw new RuntimeException("dishName的长度不能超过100");
        }
        if (dishPrice == null) {
            throw new RuntimeException("dishPrice不能为空");
        }
        if (dishPrice <= 0) {
            throw new RuntimeException("dishPrice需要大于0");
        }
        if (dishPrice > 9999.99f) {
            throw new RuntimeException("菜品价格不能超过 9999.99");
        }
        if (dishIngredients == null || dishIngredients.trim().isEmpty()) {
            throw new RuntimeException("dishIngredients不能为空");
        }
        if (dishIngredients.trim().length() > 1000) {
            throw new RuntimeException("dishIngredients的长度不能超过1000");
        }
        if (dishDescription == null || dishDescription.trim().isEmpty()) {
            throw new RuntimeException("dishDescription不能为空");
        }
        if (dishDescription.trim().length() > 500) {
            throw new RuntimeException("dishDescription的长度不能超过500");
        }
        categoryService.checkCategoryId(categoryId);
        int tempStamp = (int) (System.currentTimeMillis() / 1000);
        Dish dish = new Dish()
                .setCoverImages(coverImages.trim())
                .setDishName(dishName.trim())
                .setDishPrice(dishPrice)
                .setDishIngredients(dishIngredients.trim())
                .setDishDescription(dishDescription.trim())
                .setUpdateTime(tempStamp)
                .setCategoryId(categoryId);
        if (dishId == null) {   //插入
            dish.setCreateTime(tempStamp);
            dish.setDishScore(0);
            dish.setDishReview("暂时没有人评论");
            dish.setIsDeleted(0);
            dishMapper.insert(dish);
            return dish.getId();
        } else {    //更新
            if (dishId <= 0) {
                throw new RuntimeException("dishId需要大于0");
            }
            Dish temp = dishMapper.getById(dishId);
            if (temp == null) {
                throw new RuntimeException("被更新的菜品id不存在");
            }
            dish.setId(dishId);
            dishMapper.update(dish);
            return dishId;
        }

    }

    public int delete(Long dishId) {
        if (dishId == null) {
            throw new RuntimeException("dishId不能为空");
        }
        if (dishId <= 0) {
            throw new RuntimeException("dishId需要大于0");
        }
        Dish temp = dishMapper.getById(dishId);
        if (temp == null) {
            throw new RuntimeException("菜品不存在");
        }
        return dishMapper.delete(dishId);
    }

    public int deleteByCategoryId(Long categoryId) {
        if (categoryId == null) {
            throw new RuntimeException("categoryId不能为空");
        }
        if (categoryId <= 0) {
            throw new RuntimeException("categoryId需要大于0");
        }
        List<Dish> temp = dishMapper.getByCategoryId(categoryId);
        if (temp == null) {
            throw new RuntimeException("菜品不存在");
        }
        return dishMapper.deleteByCategoryId(categoryId);
    }

    public static final Integer PAGE_SIZE = 10;

    public List<Dish> getAllToAPP(Integer dishPage, String keyword) {
        if (dishPage == null) {
            throw new RuntimeException("dishPage不能为空");
        }
        if (dishPage <= 0) {
            throw new RuntimeException("dishPage需要大于0");
        }
        if (keyword != null && keyword.trim().isEmpty()) {
            throw new RuntimeException("keyword不能为空");
        }
        StringBuilder ids = categoryService.getIds(keyword);
        Integer offset = (dishPage - 1) * PAGE_SIZE;
        List<Dish> dbList = dishMapper.getAllToAPP(offset, PAGE_SIZE, keyword, ids);
        if (dbList == null) {
            return new ArrayList<>();
        }
        return dbList;
    }

    public List<DishDTO> getAllToConsole(Integer dishPage, String keyword) {
        if (dishPage == null) {
            throw new RuntimeException("dishPage不能为空");
        }
        if (dishPage <= 0) {
            throw new RuntimeException("dishPage需要大于0");
        }
        if (keyword != null && keyword.trim().isEmpty()) {
            throw new RuntimeException("关键字不能为空");
        }
        Integer offset = (dishPage - 1) * PAGE_SIZE;
        List<DishDTO> dbList = dishMapper.getAllToConsole(offset, PAGE_SIZE, keyword);
        if (dbList == null) {
            return new ArrayList<>();
        }
        return dbList;
    }

    public List<Dish> getByCategoryId(Long categoryId) {
        return dishMapper.getByCategoryId(categoryId);
    }

    public Integer getTotal() {
        return dishMapper.getTotal();
    }

    public Integer getLikeTotal(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return -1;
        }
        return dishMapper.getLikeTotal(keyword);
    }

    public List<Dish> getAll() {
        return dishMapper.getAll();
    }

    public void saveBatch(List<Dish> list) {
        if (list != null && !list.isEmpty()) {
            dishMapper.insertBatch(list); // 调用你写的 XML 方法
        }
    }

    public List<Dish> getDishShardByMod(int mod) {
        return dishMapper.selectByMod(mod);
    }
}
